package tourist

import (
	"net/http"

	_ "github.com/go-sql-driver/mysql"
	"ICan/hook"
	"github.com/gin-gonic/gin"
	"ICan/errmsg"
	M "ICan/MySql"
)
type phone struct {
	id string
}

func Load(ctx *gin.Context){
	phone_id := ctx.PostForm("phone_id")
	sqlstr := `select phone_id from youke where phone_id = ?`
	res := M.Db.QueryRow(sqlstr, phone_id)
	token, _ := hook.SetToken(phone_id)
	var u phone
	res.Scan(&u.id)
	if u.id != "" {
		code := errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"token":token,
		}
		ctx.JSON(http.StatusOK, data)
	}else {
		sqlstr1 := `insert into youke values (?)`
		res1, _ := M.Db.Exec(sqlstr1, phone_id)
		res1.LastInsertId()
		code := errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"token":token,
		}
		ctx.JSON(http.StatusOK, data)
	}
}